/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-06 15:08:22
 * describe:单选与多选
 */
APP.controller('RadioCheckController', ['$scope', 'RadioCheckService', 'PopupService',
  function ($scope, RadioCheckService, PopupService) {

    /***变量声明***/
    $scope.data = undefined;
    $scope.selectedData = undefined;
    $scope.selectedIndex = undefined;
    $scope.mode = undefined;
    $scope.isSelected = undefined;

    $scope.data1 = undefined;
    $scope.data1TotalSelected = undefined;
    var index = 3;
    /**
     * 初始化函数
     */
    $scope.init = function () {
      $scope.mode = {
        isSingle: true
      };
      $scope.isSelected = [];
      //重置数据
      $scope.data = dataInit($scope.mode.isSingle);
      //根据数据重置select数组
      $scope.isSelected = selectInit($scope.data);
      //选中数据
      $scope.selectedData = selectedSingleData($scope.data, $scope.isSelected);

      $scope.data1 = dataInit(true);
      $scope.data1TotalSelected = false;
    };
    /**
     * 重置数据
     */
    var dataInit = function (isSingle) {
      var data = [];
      for (var i = 0; i < index; i++) {
        data.push('测试数据' + i);
      }
      if (!isSingle) {
        data.push('全选');
        data.push('全不选');
        data.push('反选');
      }
      return data;
    };
    /**
     * 重置select数组
     */
    var selectInit = function (data) {
      var selectArray = [];
      for (var i = 0; i < data.length; i++) {
        //默认选中第一个
        if (i == 0) {
          selectArray.push(true);
        } else {
          selectArray.push(false);
        }
      }
      return selectArray;
    };
    /**
     * 单选选中数据
     */
    var selectedSingleData = function (data, selectArray) {
      var selectedData = '';
      for (var i = 0; i < data.length; i++) {
        if (selectArray[i]) {
          selectedData = data[i];
          return selectedData;
        }
      }
    };
    /**
     * 单选选中索引
     */
    var selectedSingleIndex = function (data, selectedData) {
      for (var i = 0; i < data.length; i++) {
        if (selectedData == data[i]) {
          return i;
        }
      }
    };
    /**
     * 是否全选
     */
    var isAllSelected = function (selectedData) {
      for (var i = 0; i < selectedData.length; i++) {
        if (!selectedData[i]) {
          return false;
        }
      }
      return true;
    };
    /**
     * 模式选择--单选多选切换
     */
    $scope.modeSelect = function (isSingle) {
      $scope.mode.isSingle = isSingle;
      $scope.data = dataInit(isSingle);
      //根据数据重置select数组
      //$scope.isSelected = selectInit($scope.data);
      $scope.data1 = dataInit(true);
    };
    /**
     * 选择
     */
    $scope.select = function (index) {
      //显示数据
      var data = $scope.data;
      //select数组
      var isSelected = $scope.isSelected;
      //单选或多选
      var isSingle = $scope.mode.isSingle;
      var mode = 0;//默认单选
      if (!isSingle) {
        if (index == data.length - 3) {
          mode = 2;//全选
        } else if (index == data.length - 2) {
          mode = 3;
        } else if (index == data.length - 1) {
          mode = 4;
        } else {
          mode = 1;
        }
      }
      //调用服务获取select数组结果
      $scope.isSelected = RadioCheckService.selectMode(isSelected, mode, index);
      //选中数据
      $scope.selectedData = selectedSingleData(data, isSelected);
      $scope.data1TotalSelected = isAllSelected(isSelected);
    };
    /**
     * select框选择
     */
    $scope.selectItem = function (selectedData) {
      ////选中数据
      //$scope.selectedData = selectedSingleData($scope.data, $scope.isSelected);
      //选中索引
      $scope.selectedIndex = selectedSingleIndex($scope.data, selectedData);
      $scope.select($scope.selectedIndex);
    };
    /**
     * data1全选
     */
    $scope.totalSelect = function () {
      $scope.data1TotalSelected = !$scope.data1TotalSelected;
      //select数组
      var isSelected = $scope.isSelected;
      var data1TotalSelected = $scope.data1TotalSelected;
      var mode = 0;//默认单选
      if (data1TotalSelected) {
        mode = 2;
      } else {
        mode = 3;
      }
      //调用服务获取select数组结果
      $scope.isSelected = RadioCheckService.selectMode(isSelected, mode);
    };

    /**
     * 列表选择
     */
    $scope.finish = function () {
      var showTest = '';
      //显示数据
      var data = $scope.data;
      //select数组
      var isSelected = $scope.isSelected;
      for (var i = 0; i < data.length; i++) {
        if (isSelected[i]) {
          showTest += data[i];
        }
      }
      PopupService.showToast('你选择了' + (showTest ? showTest : '空数据'));
    };

    /*
     $scope.init();//如果页面缓存则用此方法初始化
     */
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.init();
    });
  }
]);

/**
 * creater:han.wen@dhc.com.cn
 * create time:2016-06-06 15:08:22
 * describe:单选与多选
 */
APP.service('RadioCheckService', ['$http', 'UrlService', function ($http, UrlService) {
  /**
   * 选择模式
   * @param data  bool值数组
   * @param mode  模式 0-单选 1-多选 2-全选 3-全不选 4-反选
   * @param index  选择第几个
   */
  this.selectMode = function (data, mode, index) {
    var i;
    switch (mode) {
      case 0:
        for (i = 0; i < data.length; i++) {
          if (i == index) {
            if (!data[i]) {
              data[i] = true;
            }
          } else {
            if (data[i]) {
              data[i] = false;
            }
          }
        }
        break;
      case 1:
        data[index] = !data[index];
        break;
      case 2:
        for (i = 0; i < data.length; i++) {
          data[i] = true;
        }
        break;
      case 3:
        for (i = 0; i < data.length; i++) {
          data[i] = false;
        }
        break;
      case 4:
        for (i = 0; i < data.length; i++) {
          data[i] = !data[i];
        }
        break;
    }
    return data;
  }

}]);
